﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace GenericLibrary.Common {

    public class UniqueList<T> : List<T> {

        public new void Add( T item ) {
            try {
                if( !base.Contains( item ) ) {
                    base.Add( item );
                }
            } catch( Exception ) {
                Debug.Assert( false );
                throw;
            }
        }

        public new void AddRange( IEnumerable<T> collection ) {
            try {
                foreach( T item in collection ) {
                    this.Add( item );
                }
            } catch( Exception ) {
                Debug.Assert( false );
                throw;
            }
        }

        public new void Insert( int index, T item ) {
            try {
                if( !base.Contains( item ) ) {
                    base.Insert( index, item );
                }
            } catch( Exception ) {
                Debug.Assert( false );
                throw;
            }
        }

        public new void InsertRange( int index, IEnumerable<T> collection ) {
            try {
                foreach( T item in collection ) {
                    this.Insert( index++, item );
                }
            } catch( Exception ) {
                Debug.Assert( false );
                throw;
            }
        }

        public new T this[ int index ] {
            get {
                return base[ index ];
            }
            set {
                try {
                    if( !base.Contains( value ) ) {
                        base[ index ] = value;
                    }
                } catch( Exception ) {
                    Debug.Assert( false );
                    throw;
                }
            }
        }
    }
}
